<?php
include('admin.php');
set_page_info('id', 'posts');
$categories = new items(array(
	'table' => CATEGORIES,
	'class' => 'category'
));
$users = new items(array(
	'table' => USERS,
	'class' => 'user'
));

if (isset($_POST['submit'])) {
	$c = get_post_data();
	$c['date'] = mktime($c['hour'], $c['minute'], date('s'), $c['month'], $c['day'], $c['year']);
	$c['date'] -= (60 * 60 * (int)get_option('timeoffset'));
	$c['date'] = get_date($dateFormats['mysql'], $c['date']);
	
	/***TITLE AND CONTENT***/
	if (empty($c['title']) || empty($c['content'])) error('Please enter a title and some content.');
	
	/***SLUG***/
	if (empty($c['slug'])) $c['slug'] = get_slug($c['title']);
	$amount = (int)mysql_result($mysql->query('SELECT COUNT(*) FROM `'.POSTS.'` WHERE `post_title_slug` = "'.escape($c['slug']).'"'), 0);
	if ($amount > 0) $c['slug'] = $c['slug'].'-'.$amount;
	unset($amount);
	
	/***CATEGORIES***/
	$c['categories'] = (!empty($c['categories']) && is_array($c['categories']) && count($c['categories']) > 0) ? array_intersect($c['categories'], get_ids($categories)) : (array)1;
	$c['categories'] = implode(',', (count($c['categories']) > 0) ? $c['categories'] : (array)1);
	
	/***USERS***/
	if (empty($c['user']) || !in_array($c['user'], get_ids($users))) $c['user'] = currentuser()->get_id();
	
	/***CUSTOM FIELDS***/
	$c['custom_fields'] = array();
	if (!empty($c['custom_key']) && !empty($c['custom_value']) && count($c['custom_key']) > 0 && count($c['custom_value']) > 0 && count($c['custom_key']) === count($c['custom_value'])) {
		$c['custom_fields'] = array_combine($c['custom_key'], $c['custom_value']);
		foreach($c['custom_key'] as $key => $value) {
			$c['custom_fields'][$value] = $c['custom_value'][$key];
		}
	}
	
	/***COMMENTS AND STATUS***/
	$c['comments'] = (int)$c['comments'];
	if (empty($c['comments'])) $c['comments'] = 0;
	else $c['comments'] = 1;
	
	$c['status'] = (int)$c['status'];
	if (empty($c['status'])) $c['status'] = 0;
	else $c['status'] = 1;
	
	$c['content'] = clean($_POST['content'], true);
	$result = $mysql->insert(POSTS, array(
		'post_title' => $c['title'],
		'post_title_slug' => $c['slug'],
		'post_date' => $c['date'],
		'post_category' => $c['categories'],
		'post_content' => $c['content'],
		'post_excerpt' => $c['excerpt'],
		'post_status' => $c['status'],
		'post_comments' => $c['comments'],
		'post_titletag' => $c['titletag'],
		'post_metatag' => $c['metatag'],
		'post_author' => $c['user'],
		'post_custom_fields' => (count($c['custom_fields']) > 0) ? serialize($c['custom_fields']) : ''
	));
	if ($result === true) header('Location: posts.php#post-'.$mysql->lastInsertId);
	else error('There was an error saving the post.');
} else {
	set_page_info('title', 'New Post');
	get_header();
?>
		<h2>New Post</h2>
		<form action="write.php" method="post">
			<p><label for="title">Title</label>
			<input type="text" name="title" id="title" class="slug" /></p>
			
			<p><label for="slug">Slug</label>
			<input type="text" name="slug" id="slug" class="slughere" /></p>
			 <p>
            <label for "titletag">Title Tag</label>
            <input type="text" name="titletag" id="titletag" value="" />
            </p>
            
             <p>
            <label for "metatag">Meta Tag</label>
            <input type="text" name="metatag" id="metatag" value="" />
            </p>
			<p><label for="content">Content</label>
			<textarea name="content" id="content" class="large wysiwyg" rows="6" cols="60"></textarea>
            <script type="text/javascript">
				var oEdit1 = new InnovaEditor("oEdit1");
				oEdit1.cmdAssetManager="modalDialogShow('/admin/editor/assetmanager/assetmanager.php',640,445);";
				oEdit1.REPLACE("content");
			</script>
            </p>
			
			<p><label for="excerpt">Excerpt</label>
			<textarea name="excerpt" id="excerpt" class="small" rows="6" cols="60"></textarea></p>
			
			<p>
				<label>Categories</label>
<?php foreach($categories->items as $category) { ?>
				<input type="checkbox" name="categories[]" id="categories[<?php $category->the_id(); ?>]" class="checkbox" value="<?php $category->the_id(); ?>" /> <label for="categories[<?php $category->the_id(); ?>]" class="checkboxlabel"><?php $category->the_name(); ?></label>
<?php } ?>
			</p>
			
			<p>
				<label>Date</label>
				<select name="month" id="month">
<?php $current = (int)get_date('n'); for($i=1;$i<=12;$i++) { ?>
					<option value="<?php echo $i; ?>"<?php if ($i === $current) echo ' selected="selected"'; ?>><?php echo get_date('M', mktime(0, 0, 0, $i)); ?></option>
<?php } ?>
				</select>
				
				<input type="text" name="day" id="day" value="<?php echo get_date('j'); ?>" /> , <input type="text" name="year" id="year" value="<?php echo get_date('Y'); ?>" /> @ <input type="text" name="hour" id="hour" value="<?php echo get_date('H'); ?>" /> : <input type="text" name="minute" id="minute" value="<?php echo get_date('i'); ?>" />
			</p>
			
            
			<div class="paragraph">
				<label>Custom Fields</label>
				<div id="custom_fields">
					<div id="fields"></div>
					<div class="field">
						<div class="left">
							<input type="text" id="js_custom_key" />
							<input type="submit" id="js_save_custom_field" class="button" value="Save" />
						</div>
						
						<textarea id="js_custom_value" rows="3" cols="30"></textarea>
					</div>
				</div>
			</div>
			
			<p>
				<label for="comments">Allow Comments</label>
				<input type="radio" name="comments" id="comments[0]" class="radio" value="1" checked="checked" /> <label for="comments[0]" class="radiolabel">Yes</label>
				<input type="radio" name="comments" id="comments[1]" class="radio" value="0" /> <label for="comments[1]" class="radiolabel">No</label>
			</p>
			
			<p>
				<label for="comments">Status</label>
				<input type="radio" name="status" id="status[0]" class="radio" value="1" checked="checked" /> <label for="status[0]" class="radiolabel">Published</label>
				<input type="radio" name="status" id="status[1]" class="radio" value="0" /> <label for="status[1]" class="radiolabel">Draft</label>
			</p>
            
           
			
			<p>
				<label for="user">Author</label>
				<select name="user" id="user">
<?php foreach($users->items as $user) { ?>
					<option value="<?php $user->the_id(); ?>"<?php if (currentuser()->get_id() == $user->get_id()) echo ' selected="selected"'; ?>><?php $user->display_name(); ?></option>
<?php } ?>
				</select>
			</p>
			
			<p><input type="submit" name="submit" class="button" value="Save" accesskey="s" /></p>
		</form>
<?php
	get_footer();
}
?>